const router = require('koa-router')()
const userService = require('../controllers/mySqlConfig')
const utils = require('../controllers/util')

router.prefix('/note')

//
router.post('/findNoteListByType', async(ctx, next) => {
    let note_type = ctx.request.body.note_type
    await userService.catchData(note_type)
    .then(res => {
        // console.log(res);
        if(res.length) {
            let result = {res}
            ctx.body = {
                code:200,
                data:result,
                mess:'查找成功'
            }  
        } else {
            ctx.body = {
                code:2000,
                mess:'查找失败'
            }
        }
    })
})

router.get('/findNoteListByTypeID', async(ctx, next) => {
    let _id = ctx.request.query.id
    console.log(_id);
    await userService.find(_id).then(res => {
        // console.log(res);
        console.log('12333');
        if(res.length) {
            ctx.body = {
                code:200,
                data:res,
                mess:'查找成功'
            }
        } else {
            ctx.body = {
                code:2000,
                mess:'查找失败'
            }
        }
    })
})

router.post('/publishNote', async(ctx, next) => {
    let _noteContent = ctx.request.body.noteContent
    ,_noteTitle = ctx.request.body.noteTitle
    ,_noteImg =ctx.request.body.noteImg
    ,_noteType = ctx.request.body.noteType
    ,_userId= ctx.request.body.userId
    ,_nickname = ctx.request.body.nickname
    ,m_time = '2019年8月'//utils.getNowFormatDate()
    ,c_time = '2019年8月'//utils.getNowFormatDate()
    await userService.Publish([_noteContent, _noteTitle, _noteImg, _noteType, _userId, _nickname,m_time,c_time])
    .then(res => {
        console.log(res,'-----');
        if(res.affectedRows == 1) {
            ctx.body = {
                code:200,
                data:'ok',
                mess:'发布成功'
            }
        } else {
            ctx.body = {
                code:20001,
                data:'error',
                mess:'发布失败'
            }
        }
        
    })
    .catch(err => {
        ctx.body = {
          code: 2000,
          data: err
        }
      })
})
module.exports = router